$(function() {
	//改变用户名
	if (window.name) {
		$('.a1').text(window.name);
	}
	//点击图片跳转到首页
	$('.ToFirst').on('click', function() {
		location = "/HomePage.html"
	})
	if (!window.name) {
		layer.msg("亲,请登录!");
		return
	}
	//获取table里的数据
	let obj = {
		name: window.name
	};
	$.post('http://10.41.152.2:8080/obtain', obj, function(res) {
		date = res.data;
		if (date == undefined) {
			return
		}
		date.forEach((el, index) => {
			let price = el.u_price;
			var numPrice = parseFloat(price.match(/\d+\.\d+/g)[0]);
			$(`<table border="" cellspacing="" cellpadding=""><tr><td><input type="checkbox" class="check" /></td><td><img src=${el.u_img} ><p>${el.u_name}</p></td><td>会员价:<span>${el.u_price}</span></td><td></td><td><button type="button" class="sub" >-</button><input type="text"  value="${el.u_num}" class="num"/><button type="button" class="add">+</button></td><td class="sum">$${numPrice*el.u_num}</td><td><button type="button" class="del">删除</button><button type="button" class="modify">修改</button></td><tr></table>`)
				.appendTo('.two');
		})
	})
	//创建弹窗广告
	let advertisement = $('<div class="advertisement"><img src="img/kefu_pop_mid.png"></div>');
	advertisement.css({
		position: "fixed",
		left: "50%",
		bottom: "30px",
		zIndex: 888,
		marginLeft: "-600px"
	})
	advertisement.appendTo($(document.body));
	advertisement.on('click', function() {
		this.remove();
	})

	//设置小轮播事件
	var num = 0;
	setInterval(function() {
		num++;
		if (num > 3) {
			num = 0;
			$('#seach_ul').css('top', 0)
		};
		$('#seach_ul').animate({
			top: -41 * num
		})
	}, 2000)


	//给头部下标签绑定移入事件
	$(".ul2>li").on('mouseenter', function() {
		$(this).find('a').css('color', 'red')
		$(this).find('ul').show();
		$(this).find('ul').children().on('mouseenter', function() {
			$(this).css('color', 'red').siblings().css('color', '')
		})
	}).on('mouseleave', function() {
		$(this).find('a').css('color', '')
		$(this).find('ul').hide()
	})
})

$(function() {
	//未登录就无法使用功能
	if (!window.name) {
		layer.msg("亲,请登录!");
		return
	}
	//注销
	$('.ZX').on('click', function() {
		window.name = "";
	})
	//全选或全不选
	$('.cart .all').on('click', function() {
		$('.cart .one').siblings().children().find("input[type='checkbox']").prop('checked', this
			.checked);
	})
	//根据选中框数量判断全选按钮
	$('.cart .two').on('click', "input[type='checkbox']", function() {
		$(".two input:checked").length == $('.cart .two').children().length ? $('.cart .all').prop(
			'checked', true) : $('.cart .all').prop('checked', false)
	})

	//数量加减/金额计算
	$(function() {
		$('.two').on('click', '.add', function() {
			let self = $(event.target);
			let num = parseInt(self.prev().val()) + 1;
			self.prev().val(num);
			let price = self.parents('tr').children().eq(2).find('span').text();
			var numPrice = parseFloat(price.match(/\d+\.\d+/g)[0]);
			self.parent().next().text(`$${numPrice*num}`)
		})
		$('.two').on('click', '.sub', function() {
			let self = $(event.target);
			let num = parseInt(self.next().val()) - 1;
			if (num < 0) {
				num = 0
			}
			self.next().val(num);
			let price = self.parents('tr').children().eq(2).find('span').text();
			let numPrice = parseFloat(price.match(/\d+\.\d+/g)[0]);
			self.parent().next().text(`$${numPrice*num}`);
		})
	})

	//num输入框事件
	$('.two').on('blur', '.num', function() {
		console.log(111);
		let number = $(event.target).val();
		let price = $(event.target).parents('tr').children().eq(2).find('span').text();
		let numPrice = parseFloat(price.match(/\d+\.\d+/g)[0]);
		$(event.target).parent().next().text(`$${numPrice*number}`);
	})
	//删除物品
	$('.two').on('click', '.del', function() {
		if (!confirm("你确定要删除该商品么?")) {
			return
		}
		let data = {
			name: $(this).parents('tr').find('p').text(),
			uname: window.name
		}
		$.post('http://10.41.152.2:8080/del', data, function(res) {
			layer.msg(res.msg);
		})
		$(this).parents('table').remove();
	})

	//修改购物车信息
	$('.two').on('click', '.modify', function() {
		if (!confirm("你确定要修改该商品数量么?")) {
			return
		}
		let data = {
			name: $(this).parents('tr').find('p').text(),
			num: $(this).parents('tr').find('.num').val(),
			uname: window.name
		}
		$.post('http://10.41.152.2:8080/modify', data, function(res) {
			layer.msg(res.msg);
		})
	})

	//提交购物信息
	$('.three button').on('click', function() {
		let money = 0;
		if ($(".two input:checked").length < 1) {
			layer.msg("亲,未中商品哦!");
			return
		}
		$(".two input:checked").each(function(index, el) {
			money += parseInt($(el).parents('tr').find('.sum').text().split('$')[1]);
		})
		layer.msg("亲,一共消费了" + money + "元哦!");
	})
})
